package com.gxuwz.action.front;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.SysRole;
import com.gxuwz.pojo.SysRoleRight;
import com.gxuwz.pojo.SysUser;
import com.gxuwz.pojo.SysUserRole;
import com.gxuwz.service.ISysRoleRightService;
import com.gxuwz.service.ISysUserRoleService;
import com.gxuwz.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends BaseAction implements ModelDriven<SysUser>{
	
	protected static final String LOGIN_JSP = "/login.jsp";
	protected static final String MAIN_JSP = "/WEB-INF/page/main.jsp";
	

	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private ISysRoleRightService sysRoleRightService;
	
	private SysUser sysUser = new SysUser();
	
	public String webLogin(){
		//判断是否登录成功，保存用户信息
		Boolean isLogin = false;
		isLogin = sysUserService.login(sysUser);
		if(true){
			ActionContext.getContext().getSession().put("sysUser", sysUser);
//			List<String> listRight = new ArrayList<String>();
////			查询对应角色
//
//			SysUserRole userRole = new SysUserRole();
//			userRole.setUserId(sysUser.getUserId());
//			List<SysUserRole> userRoleList = sysUserRoleService.findByExample(userRole);
//			for(SysUserRole ur: userRoleList){
////				查询角色对应权限，保存权限信息
//				SysRoleRight rr = new SysRoleRight();
//				rr.setRoleId(ur.getRoleId());
//				List<SysRoleRight> rrList = sysRoleRightService.findByExample(rr);
//				for(SysRoleRight srr: rrList){
//					listRight.add(srr.getRightId());
//				}
//			}
//			ActionContext.getContext().getSession().put("listRight", listRight);
			getRequest().getSession().setAttribute("userId",sysUser.getUserId() );
			
			setForwardView(MAIN_JSP);
		}else{
			setForwardView(LOGIN_JSP);
		}


		
		return SUCCESS;
	}

	@Override
	public SysUser getModel() {
		// TODO Auto-generated method stub
		return sysUser;
	}

	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}
	
	
}
